<template>
	<view class="result">
		<text v-if="tip && tip != 'undefined'" class="t_tip">{{tip}}</text>
		<view class="top">
			<image :src="result_img" mode="aspectFit" v-if="result_img" class="ii" @click="onImgClick"></image>
		</view>
		
		<button class="btn" @click="saveImageToAlbum(result_img)" :loading="loadding" :disabled="loadding">保存</button>
		<uni-popup ref="popup" type="center">
			<view class="popup-content">
				<uni-icons type="closeempty" size="20" style="align-self: flex-end;" @click="closepop"></uni-icons>
				<view class="popup-content2">
					<uni-icons type="checkbox-filled" size="50" color="#1aad19"></uni-icons>
					<text style="margin-top: 20rpx;font-size: 35rpx;">已保存到相册</text>
					<button type="default" size="mini" style="margin-top: 40rpx;width: 270rpx;"
						open-type="share">分享小程序</button>
					<button type="primary" size="mini" style="margin-top: 30rpx;"
						@click="showhaoping">五星好评，永久免费！</button>
				</view>

			</view>
		</uni-popup>

	</view>
</template>

<script>
	import permision from '@/js_sdk/wa-permission/permission.js';

	export default {
		data() {
			return {
				result_img: null,
				tip: null,
				loadding: false
			};
		},
		onLoad(options) {
			this.result_msg = decodeURIComponent(options.result_msg)
			this.result_img = decodeURIComponent(options.result_img)
			this.tip = options.tip;
		},
		methods: {
			onImgClick() {
				uni.previewImage({
					urls: [this.result_img]
				});
			},
			async checkpromission() {
				// #ifdef APP
				let promise = permision.judgeIosPermission('photoLibrary');
				return promise;
				// #endif
				// #ifdef MP-WEIXIN
				const [fail, success] = await uni.authorize({
					scope: 'scope.writePhotosAlbum'
				});
				console.log(fail, success);
				// #endif
			},

			saveImageToAlbum(imageUrl) {
				// if (!this.checkpromission()){
				// 	this.$showToast('请给相册权限')
				// 	return
				// }
				this.loadding = true;
				let _this = this;
				uni.downloadFile({
					url: imageUrl,
					success: (downloadResult) => {
						// c
						if (downloadResult.statusCode === 200) {
							// 将下载的图片保存到相册
							uni.saveImageToPhotosAlbum({
								filePath: downloadResult.tempFilePath,
								success: () => {
									// #ifdef MP-WEIXIN || MP-ALIPAY
									_this.showpop()
									return
									// #endif
									uni.showToast({
										title: '图片保存成功'
									});
								},
								fail: (err) => {
									// 权限问题或其他原因导致保存失败
									console.error('saveImageToPhotosAlbum fail', err);
									uni.showToast({
										icon: 'none',
										title: '图片保存失败，请检查权限设置'
									});
								}
							});
						} else {
							console.log(downloadResult);
							// 响应的HTTP状态码不是200
							uni.showToast({
								icon: 'none',
								title: '下载失败，HTTP状态码：' + downloadResult.statusCode
							});
						}
					},
					fail: () => {
						uni.showToast({
							icon: 'none',
							title: '下载失败，请重试'
						});
					},
					complete: () => {
						_this.loadding = false;
					}
				});
			},
			showpop() {
				this.$refs.popup.open('center')
			},
			closepop() {
				this.$refs.popup.close()
			},
			showhaoping() {
				this.closepop()
				uni.showModal({
					title: "五星好评永久免费",
					content: "请点击右上角“...”进行评价，\n 给予5星好评！",
					showCancel: true
				})
			},
			onShareAppMessage(res) { //发送给朋友
				return {
					title: "一键Ai换脸，视频换脸，图片换脸，Ai写真，快来体验吧",
					imageUrl: "https://sm.2sxzw.com/mgmg/jiabin01/mine/fenxiang.jpg",
				}
			},
			onShareTimeline(res) { //分享到朋友圈
				return {
					title: "一键Ai换脸，视频换脸，图片换脸，Ai写真，快来体验吧",
					imageUrl: "https://sm.2sxzw.com/mgmg/jiabin01/mine/fenxiang.jpg",
				}
			},
		}
	};
</script>

<style lang="scss">
	@import 'Result.scss';
</style>